﻿using Architecture.API.Application.Command.RoleCommand;
using Architecture.Domain;
using Architecture.Domain.Service.RoleServices;
using MediatR;

namespace Architecture.API.Application.CommandHandlers.RoleHandler
{
    /// <summary>
    /// 角色创建命令处理器
    /// int 返回值类型
    /// </summary>
    public class RoleCreateCommandHandler : IRequestHandler<RoleCreateCommand, int>
    {
        /// <summary>
        /// 角色服务
        /// </summary>
        private readonly IRoleService _roleService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleService">角色服务</param>
        public RoleCreateCommandHandler(IRoleService roleService)
        {
            _roleService = roleService;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消标记</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Handle(RoleCreateCommand request, CancellationToken cancellationToken)
        {
            return await _roleService.AddRole(new Role() { RoleName = request.RoleName });
        }

    }
}
